﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ArtistMusic : System.Web.UI.Page
{
    static int pageSize = 10;
    static int aid;

    protected void Page_Load(object sender, EventArgs e)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        string aid_s = Request.QueryString["aid"];
        bool t = Int32.TryParse(aid_s, out aid);

        if (t)
        {
            LoadMusic(1);
        }
    }

    private void LoadMusic(int pid)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        titleDetails.InnerText = "Music by \"" + db.Artists.Where(i => i.ArtistID == aid).First().ArtistName + "\"";

        var musiclst = (from i in db.Musics
                        where i.Album.ArtistID == aid
                        select new
                        {
                            MusicID = i.MusicID,
                            Title = i.Title,
                            AlbumID = i.AlbumID,
                            AlbumName = i.Album.AlbumName,
                            ArtistID = i.Album.ArtistID,
                            ArtistName = i.Album.Artist.ArtistName,
                            Rating = i.Rating,
                            Cover = i.Cover,
                            Date = i.Album.Date
                        }).OrderByDescending(i => i.Date).Skip((pid - 1) * pageSize).Take(pageSize);

        Rpt_MusicList.DataSource = musiclst;
        Rpt_MusicList.DataBind();

        int numItems = db.Musics.Where(i => i.Album.ArtistID == aid).Count();
        int numPages = numItems / pageSize;
        if (numItems % pageSize != 0)
        {
            numPages++;
        }
        BuildPaggingControls(numPages);
    }

    private void BuildPaggingControls(int numPages)
    {
        paggingControls.Controls.Clear();
        if (numPages > 1)
        {
            for (int i = 1; i <= numPages; i++)
            {
                LinkButton lnk = new LinkButton();
                lnk.Text = " " + i + " ";
                lnk.CommandArgument = i.ToString();
                lnk.Command += new CommandEventHandler(LoadPage);
                paggingControls.Controls.Add(lnk);
            }
        }
    }

    protected void LoadPage(object sender, CommandEventArgs e)
    {
        int pid = Convert.ToInt32(e.CommandArgument);
        LoadMusic(pid);
    }
}